package com.example.prison.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.example.prison.config.Response;
import com.example.prison.entity.CarAlarmRecord;
import com.example.prison.service.CarAlarmRecordService;
import com.example.prison.service.SystemLogService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api(tags = "车辆报警记录")
@RestController
@RequestMapping("/api")
public class CarAlarmRecordController {

    @Autowired
    private CarAlarmRecordService carAlarmRecordService;

    @Autowired
    private SystemLogService systemLogService;

    @ApiOperation("获取车辆报警记录列表")
    @GetMapping("/getCarAlarmRecord")
    public Response<PageInfo<CarAlarmRecord>> getCarAlarmRecord(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                                @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                                @Parameter(description = "车牌号")@RequestParam(required = false) String carNumber,
                                                                @Parameter(description = "是否处理(未处理/已处理)")@RequestParam(required = false) String solve,
                                                                @Parameter(description = "开始时间(YYYY-MM-DD HH:MM)")@RequestParam(required = false,defaultValue = "0000-00-00 00:00") String beginTime,
                                                                @Parameter(description = "结束时间(YYYY-MM-DD HH:MM)")@RequestParam(required = false,defaultValue = "9999-12-31 23:59") String endTime) {
        PageInfo<CarAlarmRecord> pageInfo = carAlarmRecordService.get(pageNumber, pageSize, carNumber, beginTime, endTime);
        return Response.success("获取成功！", pageInfo);
    }

    @ApiOperation("新增车辆报警记录")
    @PostMapping("/addCarAlarmRecord")
    public Response<?> addCarAlarmRecord(@RequestBody CarAlarmRecord record) {
        int result = carAlarmRecordService.add(record);
        log.info("新增车辆报警记录"+", 数据："+ JSON.toJSONString(record));
        systemLogService.add("新增车辆报警记录");
        return Response.success("添加成功！");
    }

    @ApiOperation("删除车辆报警记录")
    @PostMapping("/deleteCarAlarmRecord")
    public Response<?> deleteCarAlarmRecord(@Parameter(description = "ID") @RequestParam Integer[] ids) {
        carAlarmRecordService.delete(ids);
        log.info("删除车辆报警记录");
        systemLogService.add("删除车辆报警记录");
        return Response.success("删除成功！");
    }

    @ApiOperation("处理车辆报警记录信息")
    @PostMapping("/handleCarAlarmRecord")
    public Response<?> handleCarAlarmRecord(@Parameter(description = "ID")@RequestParam Integer id,
                                            @Parameter(description = "处理人")@RequestParam String handle,
                                            @Parameter(description = "处理信息")@RequestParam(defaultValue = "") String handleMessage) {
        carAlarmRecordService.handle(id,handle,handleMessage);
        log.info("处理报警记录信息"+", 数据："+JSON.toJSONString(carAlarmRecordService.getById(id)));
        systemLogService.add("处理报警记录信息");
        return Response.success("处理成功！");
    }

    @ApiOperation("依据id获取车辆报警记录信息")
    @GetMapping("/getCarAlarmRecordById")
    public Response<CarAlarmRecord> getCarAlarmRecordById(@Parameter(description = "ID")@RequestParam Integer id) {
        CarAlarmRecord record = carAlarmRecordService.getById(id);
        if(ObjectUtils.isEmpty(record)) {
            return Response.error("此id不存在！");
        }
        return Response.success("获取成功！", record);
    }

}

